<?php 
/**
 * 与html相关的操作
 * @author chengx nixgnehc@163.com
 */
class ParsingHtml
{
    /**
     * 查找html中的img标签
     * @param  [type] $content [description]
     * @return [type]          [description]
     */
    public function htmlImgs($content)
    {
        $result = array();
        $imgBeginFlag = '<img';
        $imgEndFlag = "/>";

        while (true) {
            $content = stristr($content, $imgBeginFlag);
            if ($content) {
                $result[] = stristr($content, $imgEndFlag, true) . '/>';
                $content = stristr($content, $imgEndFlag);
            } else {
                break;
            }
        }
        return $result;
    }

    /**
     * 返回html中img的url
     * @param  [type] $content  [description]
     * @param  [type] $hostInfo [description]
     * @return [type]           [description]
     */
    public function getImgUrl($content, $hostInfo = '')
    {
        $urls = array();
        // if (is_null($hostInfo)) {
        //     $hostInfo = Yii::app()->request->hostInfo;
        // }
        $imgs = $this->htmlImgs($content);
        if (!empty($imgs)) {
            foreach ($imgs as $key => $value) {
                $value = stristr($value, 'src="');
                $value = substr($value, 5);
                $temp = stristr($value, '"', true);
                if (strpos($temp, '/images/') === 0) {
                    $urls[] = $hostInfo . $temp;
                } else {
                    $urls[] = $temp;
                }
            }
        }
        return $urls;
    }

    public function filterHtmlTag($content)
    {
        $content = strip_tags($content);
        $content = str_replace('&nbsp;', '', $content);
        $content = str_replace('&gt;', '', $content);
        // TODO..
        return $content;
    }
}